var Gift = {
		changeGiftType : function(){
	        var giftType = $('#gift_type').val();
            if(giftType==1){
                $('#new_gift').hide();
                $('#product_select').show();
            } else if(giftType==2){
    	        $('#new_gift').show();
    	        $('#product_select').hide();
            }
        },
        
        openProductDialog: function(baseUrl){
            var url = baseUrl + "/inventory/product/searchitem/from/gift";
            Common.showDialog({
                url : url ,
                height : 450 ,
                width : 930 ,
                dataDiv : "productDialog"
            });
        },
        
        searchItem: function(items){
            var productHtml = "";
            var productTitle = $('#product_name').val();
            if(items[0]){
              //  var selectedItems = $("input[name='itemCkb']");
            	productHtml += "<div class='form_line_div'>";
            	productHtml += "<div class='form_element_label' style='width: 180px;'>"+productTitle+"</div>";
            	productHtml += "<div class='form_element' style='font-size:12px;color:#505050;'>"+items[0].title+"</div>";
            	productHtml += "<input type='hidden' name='product_item_id' value='"+items[0].id+"' id='product_item_id' />";
            	productHtml += "<input type='hidden' name='product_title' value='"+items[0].title+"' />";
            	productHtml += "</div><div class='clear'></div>"; 
            //	productHtml += "<div class='form_line_div'>";
            //    productHtml += "<div id='productErrorLabel' class='form_element_label' style='width: 180px;'></div>";
           //     productHtml += "<div id='productError' class='form_element_error'></div>";
           //     productHtml += "</div><div class='clear'></div>"; 
            }
            $("#product_info").html(productHtml);
            Common.closeDialog("productDialog");
        },
        
        validEmpty : function(value,label){
        	$('#'+label+'Error').html('');
            if(ValidationUtil.isBlank(value)){
                var emptyErr = $('#'+label+'_empty').val();
                $('#'+label+'Error').text(emptyErr);
                return false;
            } 
                return true;
        },
        
        validInteger : function(value,label){
        	if(!Gift.validEmpty(value, label)){
        		return false;
        	}
            if(!ValidationUtil.isInteger(value) && value!=0){
                var errInfo = $('#'+label+'_err').val();
                $('#'+label+'Error').text(errInfo);
                return false;
            } 
                return true;
        	
        },
        
        saveGift : function(){
        	var result = true;
        	var giftType = $('#gift_type').val();
        	$('.form_element_error').css({"margin-left":"180px","margin-bottom":"5px"});
        	if(giftType == 1){
        		var productItemId = $('#product_item_id').val();
        		if(productItemId == 'undefined'){
        			productItemId = '';
        		}
        		if(!Gift.validEmpty(productItemId,'product')){
        			result = false;
        		}
        	} else if(giftType == 2){
        		var title = $.trim($('#title').val());
        		if(!Gift.validEmpty(title,'title')){
        			result = false;
        		}
        		//var imagePath = $.trim($('#image_path').val());
        	//	if(!Gift.validEmpty(imagePath,'image_path')){
        	//		result = false;
        	//	}
        		
        	}
        	var exhaustPoint = $.trim($('#exhaust_point').val());
        	
        	if(!Gift.validInteger(exhaustPoint,'exhaust_point')){
        		result = false;
        	}
        	var minACPoint = $.trim($('#min_ac_point').val());
        	
        	if(!Gift.validInteger(minACPoint,'min_ac_point')){
        		result = false;
        	}
        	var startDate = $('#start_date').val();
        	if(!Gift.validEmpty(startDate,'start_date')){
        		result = false;
        	}
        	var endDate = $('#end_date').val();
        	if(!Gift.validEmpty(endDate,'end_date')){
        		result = false;
        	}
        	
        	if(!result){
        		return;
        	}
        	
        	$('#gift_form').submit();
        	
        },
        
   // For apply gift log status change
        changApplyGiftStatus : function(){
        	var curStatus = $('#cur_status').val()
        	var chStatus = $('#apply_gift_status').val();
        	if((curStatus == 2 || curStatus == 3) && chStatus == 4 ){
        		jAlert($('#canNotCancelExchant').val());
        		$('#apply_gift_status').val(curStatus);
        	}
        }
        	
}